package org.hackystat.timeline.resource.timelineproperties;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.hackystat.timeline.resource.timelineproperties.jaxb.Properties;
import org.hackystat.timeline.resource.timelineproperties.jaxb.Property;
import org.hackystat.timeline.resource.timelineproperties.jaxb.Properties.AnalysisType;
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.DomRepresentation;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;
import org.w3c.dom.Document;

/**
 * Resourse containing the properties.
 * 
 * @author Alexey R. Olkov
 * 
 */
public class TimelinePropertiesResource extends Resource {

  String userKey, projectName, sensorDataType;

  /**
   * Properties.
   */
  Properties properties = new Properties();

  /**
   * Overriden constructor.
   * 
   * @param context Context
   * @param request Request
   * @param response Response
   */
  public TimelinePropertiesResource(Context context, Request request, Response response) {
    super(context, request, response);

    System.out.println("TIMELINE PROPERTIES RESOURCE IS ACTIVE");

    // Set Variant
    this.getVariants().add(new Variant(MediaType.TEXT_XML));

    // Extract all data
    userKey = (String) request.getAttributes().get("userkey");
    projectName = (String) request.getAttributes().get("projectname");
    sensorDataType = (String) request.getAttributes().get("sensordatatype");

    // Set default display parameters
    // They will be changed from the GWT element.. But for now they will be simply accessed via
    // browser request URI (GET)

    String visible = "visible";
    String color = "color";

    properties.setOwner("http://hackystat.ics.hawaii.edu/hackystat/user/9fjchekcl34s");

    AnalysisType analysisType = new AnalysisType();
    analysisType.setName("Commit");
    Property property = new Property();
    property.setKey(visible);
    property.setValue(Boolean.TRUE.toString());
    property.setKey(color);
    property.setValue("000000");
    analysisType.getProperty().add(property);
    properties.getAnalysisType().add(analysisType);

    analysisType = new AnalysisType();
    analysisType.setName("UnitTest-passed");
    property = new Property();
    property.setKey(visible);
    property.setValue(Boolean.TRUE.toString());
    property.setKey(color);
    property.setValue("339900");
    analysisType.getProperty().add(property);
    properties.getAnalysisType().add(analysisType);

    analysisType = new AnalysisType();
    analysisType.setName("DevTime");
    property = new Property();
    property.setKey(visible);
    property.setValue(Boolean.TRUE.toString());
    property.setKey(color);
    property.setValue("0000FF");
    analysisType.getProperty().add(property);
    properties.getAnalysisType().add(analysisType);

    analysisType = new AnalysisType();
    analysisType.setName("UnitTest-failed");
    property = new Property();
    property.setKey(visible);
    property.setValue(Boolean.TRUE.toString());
    property.setKey(color);
    property.setValue("FF0000");
    analysisType.getProperty().add(property);
    properties.getAnalysisType().add(analysisType);

    analysisType = new AnalysisType();
    analysisType.setName("Unknown");
    property = new Property();
    property.setKey(visible);
    property.setValue(Boolean.TRUE.toString());
    property.setKey(color);
    property.setValue("FFFFFF");
    analysisType.getProperty().add(property);
    properties.getAnalysisType().add(analysisType);

    // color="#0000FF"' Blue
    // color="#FF0000"' Red
    // color="#339900"' Green
    // color="#993399"' Purple
    // color="#000099"' Dark Blue
    // color="#990000"' Dark Red
    // color="#CCCCCC"' Gray
    // color="#000000"' Black
    // color="#FF33FF"' Pink

    // ' Light Colors
    // color="#FFCCCC"' Light Red
    // color="#CCFFCC"' Light Green
    // color="#FFFF33"' Yellow
    // color="#FFFFFF"' White
    // color="#33FFFF"' Light Blue
    // color="#F5F5F5"' Light Gray

    // NOW READ IT FROM THE FILE

    try {
      System.out.println("Trying to open the file");
      JAXBContext jaxbContext = JAXBContext
          .newInstance("org.hackystat.timeline.resource.timelineproperties.jaxb");
      Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
      File propFile = new File("xml\\" + projectName + "\\" + userKey + "\\" + "properties.xml");
      properties = (Properties) unmarshaller.unmarshal(propFile);
      System.out.println("File is parsed");

    }
    catch (JAXBException e) {
      System.out.println("Error. The default properties will be used.");
    }
  }

  /**
   * Returns the representation of the TimelineProperties resource.
   * 
   * @param variant The representational variant requested.
   * @return The representation.
   */
  @Override
  public Representation getRepresentation(Variant variant) {
    System.out.println("Getting representation");
    DocumentBuilder builder;
    try {
      builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document doc = builder.newDocument();
      JAXBContext jaxbContext = JAXBContext
          .newInstance("org.hackystat.timeline.resource.timelineproperties.jaxb");
      System.out.println("VISIBLE:"
          + properties.getAnalysisType().get(0).getProperty().get(0).getKey());
      Marshaller marshaller = jaxbContext.createMarshaller();
      marshaller.marshal(properties, doc);
      DomRepresentation result = new DomRepresentation(MediaType.TEXT_XML, doc);
      return result;
    }
    catch (Exception e) {
      return new StringRepresentation("PROBLEM:" + e.getLocalizedMessage());
    }

  }

  /**
   * Returns a user key.
   * 
   * @return User key
   */
  String getUserKey() {
    return this.userKey;
  }

  /**
   * Returns a project name.
   * 
   * @return project name
   */
  String getProjectName() {
    return this.projectName;
  }

  /**
   * Returns a sensor data type.
   * 
   * @return sensor data type
   */
  String getSensorDataType() {
    return this.sensorDataType;
  }

}
